package me.hys.action.auth;

import me.hys.base.AjaxResult;
import me.hys.base.BaseAction;
import me.hys.entity.system.UserEntity;
import me.hys.kits.MD5Kit;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * Author: @author HYS . <br>
 * Date: 2015-9-9 下午9:25:55 . <br>
 * Function:
 */
@Controller
@RequestMapping("/")
public class AuthAction extends BaseAction {
	/**
	 * @Title : auch
	 * @Description : 登录验证
	 */
	@RequestMapping("/auth")
	@ResponseBody
	public AjaxResult auth(@ModelAttribute UserEntity user) {
		AjaxResult result = new AjaxResult();
		Subject subject = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken(
				user.getNickname(), MD5Kit.encrypt(user.getPassword()));
		try {
			subject.login(token);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMsg(e.getMessage());
		}
		return result;
	}

	/**
	 * @Title : login
	 * @Description : 登录页
	 */
	@RequestMapping("/login")
	public ModelAndView login() {
		return new ModelAndView("login");
	}

	/**
	 * @Title : ModelAndView
	 * @Description : 退出登录
	 */
	@RequestMapping("/logout")
	public String ModelAndView() {
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		return "redirect:login";
	}
}
